//
//  NemoKeyBoard_InTalking.m
//  NemoSDKDemo
//
//  Created by 杨旭东 on 2017/8/17.
//  Copyright © 2019 JackYang. All rights reserved.
//

#import "NemoKeyBoard_InTalking.h"

@interface NemoKeyView ()

@property (nonatomic, copy) void (^PressKeyBlock)(NSString *);

@property (nonatomic, assign) CGSize defaultSize;

@property (nonatomic, strong) UILabel *valueLabel;

@end

@implementation NemoKeyView

+ (instancetype)addNemoKeyView:(CGPoint)origin value:(NSString *)value pressKey:(void (^)(NSString *))press{
    NemoKeyView *key = [[NemoKeyView alloc] initWithOrigin:origin value:value];
    key.PressKeyBlock = press;
    return key;
}

- (CGSize)defaultSize{
    return CGSizeMake(49, 49);
}

- (instancetype)initWithOrigin:(CGPoint)origin value:(NSString *)value{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(origin.x, origin.y, self.defaultSize.width, self.defaultSize.height);
        self.layer.cornerRadius = self.defaultSize.width * .5f;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        
        _valueLabel = [[UILabel alloc] initWithFrame:self.bounds];
        _valueLabel.text = value;
        _valueLabel.textAlignment = NSTextAlignmentCenter;
        _valueLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:.8f];
        _valueLabel.font = [UIFont systemFontOfSize:18];
        [self addSubview:_valueLabel];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:.15];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.backgroundColor = [UIColor clearColor];
    });
    self.PressKeyBlock(_valueLabel.text);
}

@end






@interface NemoKeyBoard_InTalking () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *keyboardBackground;

@property (weak, nonatomic) IBOutlet UITextField *inputResultTextfield;

@property (nonatomic, strong) NSArray *values;

@property (nonatomic, copy) void (^ResultCallback)(NSString *);

@property (nonatomic, assign) int count;

@end

@implementation NemoKeyBoard_InTalking

+ (NemoKeyBoard_InTalking *)showNemoKeyBoard_InTalking{
    NemoKeyBoard_InTalking *keyboard = [[NSBundle mainBundle] loadNibNamed:@"NemoKeyBoard_InTalking" owner:nil options:nil].firstObject;
    keyboard.frame = [UIScreen mainScreen].bounds;
    return keyboard;
}

- (NemoKeyBoard_InTalking *(^)(void (^)(NSString *)))result{
    return ^(void (^resultCallback)(NSString *)){
        self.ResultCallback = resultCallback;
        return self;
    };
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    _count = 0;
    _keyboardBackground.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.8f];
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(15, _inputResultTextfield.bounds.size.height - .5f, _inputResultTextfield.bounds.size.width - 30, .5f);
    layer.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:.8f].CGColor;
    [_inputResultTextfield.layer addSublayer:layer];
    _inputResultTextfield.textColor = [[UIColor whiteColor] colorWithAlphaComponent:.8f];
    
    int index = 0;
    for (NSString *value in self.values) {
        CGFloat originX,originY;
        int row = index % 3;
        switch (row) {
            case 0:
                originX = 25;
                break;
                
            case 1:
                originX = (_keyboardBackground.bounds.size.width - 49) * .5f;
                break;
                
            default:
                originX = _keyboardBackground.bounds.size.width - 49 - 25;
                break;
        }
        
        int line = index / 3;
        originY = 73 + (9 + 49) * line;
        
        __weak NemoKeyBoard_InTalking *weakself = self;
        NemoKeyView *key = [NemoKeyView addNemoKeyView:CGPointMake(originX, originY) value:value pressKey:^(NSString *key) {
            if (weakself.count == 0) {
                CGSize size = [weakself.inputResultTextfield.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:21]}];
                if (weakself.inputResultTextfield.bounds.size.width - (size.width + 10) < 21) {
                    weakself.count = (int)weakself.inputResultTextfield.text.length;
                }
                
                weakself.inputResultTextfield.text = [weakself.inputResultTextfield.text stringByAppendingString:key];
            }else{
                NSString *result = [weakself.inputResultTextfield.text stringByAppendingString:key];
                weakself.inputResultTextfield.text = [result substringWithRange:NSMakeRange(weakself.inputResultTextfield.text.length - weakself.count + 1, weakself.count)];
            }
            weakself.ResultCallback(key);
        }];
        [_keyboardBackground addSubview:key];
        
        index = index + 1;
    }
}

- (NSArray *)values{
    return @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"*",@"0",@"#"];
}

- (void)dismiss{
    [self removeFromSuperview];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    id hitView = [super hitTest:point withEvent:event];
    if (hitView == self) {
        [self dismiss];
        return nil;
    }else return hitView;
}

- (void)dealloc{
    
}

@end
